<!--
    测试页
    url: "/pages/Test/test"
-->
<template>
    <view>
        <uni-row>
            <uni-col :span="12" :offset="6">
                <button @click="login" class="btn">Login</button>
            </uni-col>
        </uni-row>
        <uni-row>
            <uni-col :span="20" :offset="2">
                <uni-col :span="14">
                    <input class="input" type="number" v-model="count" placeholder="u => u.id <= id)"/>
                </uni-col>
                <uni-col :span="10">
                    <button @click="getUserInfo" class="btn">GetUserInfo</button>
                </uni-col>
            </uni-col>
        </uni-row>
        <uni-row>
            <uni-col :span="24">
                <uni-table :border="false" :stripe="true" :loading="loading" :emptyText="emptyText">
                    <uni-tr>
                        <uni-th align="center">姓名</uni-th>
                        <uni-th align="center">创建日期</uni-th>
                    </uni-tr>
                    <uni-tr v-for="(item, index) in tableData" :key="index">
                        <uni-td align="center">{{ item.nickName }}</uni-td>
                        <uni-td align="center">{{ item.createTime }}</uni-td>
                    </uni-tr>
                </uni-table>
            </uni-col>
        </uni-row>
    </view>
</template>

<script>
import UniRow from "../../uni_modules/uni-row/components/uni-row/uni-row";
import UniCol from "../../uni_modules/uni-row/components/uni-col/uni-col";
import storage from "../../common/storage/storageHelper";
import UniTd from "../../uni_modules/uni-table/components/uni-td/uni-td";
import {dateHelper} from "../../common/util/commonTools";

export default {
    name: "test",
    components: {
        UniTd,
        UniCol,
        UniRow
    },
    data() {
        return {
            userInfo: {
                nickName: "",
                avatarUrl: "",
                city: "",
                province: ""
            },
            count: null,
            tableData: [],
            loading: false,
            emptyText: "暂无更多数据"
        }
    },
    methods: {
        login() {
            let _this = this;
            uni.getUserProfile({
                lang: "zh_CN",
                desc: "登录",
                success(data) {
                    _this.userInfo.nickName = data.userInfo.nickName;
                    _this.userInfo.avatarUrl = data.userInfo.avatarUrl;
                    _this.userInfo.city = data.userInfo.city;
                    _this.userInfo.province = data.userInfo.province;
                    if (_this.userInfo != null) {
                        wx.login({
                            async success(data) {
                                await _this.$http.post(
                                    _this.$api.userLogin.url,
                                    {
                                        code: data.code,
                                        nickName: _this.userInfo.nickName,
                                        avatarUrl: _this.userInfo.avatarUrl,
                                        city: _this.userInfo.city,
                                        province: _this.userInfo.province
                                    }
                                ).then(
                                    response => {
                                        if (storage.get("token")) {
                                            uni.showToast({
                                                title: "登录成功",
                                                icon: "success",
                                                duration: 2500
                                            });
                                        } else {
                                            uni.showToast({
                                                title: "登录失败",
                                                icon: "error",
                                                duration: 2500
                                            });
                                        }
                                    }
                                ).catch(
                                    error => {
                                        console.error("登录接口请求失败：", error);
                                    }
                                );
                            },
                        });
                    }
                },
            });
        },
        getUserInfo() {
            if (this.count === null) {
                uni.showToast({
                    title: "请输入参数",
                    icon: "error",
                    duration: 2000
                });
            } else {
                this.loading = true;
                this.$http.get(
                    this.$api.UserSelectUserInfo.url,
                    {
                        id: this.count
                    }
                ).then(
                    response => {
                        if (response.statusCode === 200) {
                            response.data.forEach((item) => {
                                let data = {
                                    nickName: item.nickName,
                                    createTime: dateHelper.unixTimeStamp13ToBeijingTime("yyyy-mm-dd HH:MM:SS", item.createTime)
                                };
                                this.tableData.push(data);
                            });
                            this.loading = false;
                        } else {
                            this.loading = false;
                            this.emptyText = "错误"
                        }
                    }
                ).catch(
                    error => {
                        this.loading = false;
                        this.emptyText = error;
                    }
                );
            }
        },
    },
}
</script>

<style scoped lang="less">
.btn {
    margin-top: 5px;
    margin-bottom: 5px;
}

.input {
    border: 1px solid #c5c5c5;
    margin: 10rpx;
    height: 80rpx;
    border-radius: 8rpx;
    box-sizing: border-box;
    text-align: center;
}
</style>